/*
 *	OpenSource code or fragments by agile webentwicklung, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'avocaod systems, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 */
package de.fuhrmeister.browserchooser.controller;

import de.fuhrmeister.browserchooser.model.BrowserVersionModel;
import de.fuhrmeister.browserchooser.view.BrowserVersionView;
import de.fuhrmeister.util.ConfigManager;
import de.fuhrmeister.util.Tools;

/**
 * Controller class handling the views.
 * 
 * @date 20.11.2008
 * @author Marcus Fuhrmeister
 */
public class BrowserVersionController {

	private static BrowserVersionController self;

	/**
	 * Singleton pattern.
	 * 
	 * @return this
	 */
	public static synchronized BrowserVersionController getInstance() {
		if (self == null) {
			BrowserVersionController.self = new BrowserVersionController();
			return BrowserVersionController.self;
		}
		return BrowserVersionController.self;
	}

	/**
	 * Calls to create a new <code>BrowserVersionView</code> with the given id and
	 * the settings in the <code>BrowserVersionModel</code>.
	 * 
	 * @param browserID the browser id
	 * @param bvm the BrowserVersionModel
	 * @return the BrowserVersionView
	 */
	public BrowserVersionView getView(final int browserID, final BrowserVersionModel bvm) {
		final BrowserVersionView bvv = this.createView(browserID);
		bvv.setBrowserExec(bvm.getExec());
		bvv.setBrowserVersion(bvm.getVersion());
		return bvv;
	}

	/**
	 * Calls to create a new <code>BrowserVersionView</code> with the given id and
	 * a null parameter for the executable path.
	 * 
	 * @param id the browser id
	 * @return the BrowserVersionView
	 */
	public BrowserVersionView createView(final int id) {
		return this.createView(id, null);
	}

	/**
	 * Creates a new <code>BrowserVersionView</code> with the given id and
	 * the executable path.
	 * 
	 * @param id the browser id
	 * @param absolutePath the executable path
	 * @return the BrowserVersionView
	 */
	public BrowserVersionView createView(final int id, final String absolutePath) {
		final BrowserVersionView newBrowserVersionView = new BrowserVersionView(Integer.parseInt(Tools.configMng.getConfiguration(ConfigManager.SETTINGS_SIZE_WIDTH)));
		newBrowserVersionView.setBrowserID(id);
		newBrowserVersionView.setBrowserVersionProperties(Tools.configMng.getStringFor(ConfigManager.STRING_LABEL_BROWSERVERSION));
		newBrowserVersionView.setBrowserExecProperties(Tools.configMng.getStringFor(ConfigManager.STRING_LABEL_BROWSEREXEC), Tools.configMng.getStringFor(ConfigManager.STRING_BUTTON_BROWSE));
		newBrowserVersionView.setBrowserDelete(Tools.configMng.getStringFor(ConfigManager.STRING_BUTTON_DELETE));
		newBrowserVersionView.setBrowserExec((absolutePath != null) ? absolutePath : "");
		return newBrowserVersionView;
	}
}